const that = this
const variables = this.getVariables();
const data = variables.find(v => v.name === "screen_params")?.value;
const prefix = variables.find(v => v.name === 'environmentType')?.value || '';

const gis = this.getApiWidgetByAliasName('三维组件');
const disposition = this.getApiWidgetByAliasName('处置方案')?.widgetObj;
const eventDetail = this.getApiWidgetByAliasName('事件详情')?.widgetObj;
const eventApi = this.retrievePlugin('eventApi');

const $form = this.getApiWidgetByAliasName('judge-content');
const $img = this.getApiWidgetByAliasName('detail-img');
const levels = this.getDataById('744574925553008640');
const types = this.getDataById('744575296061046784');
const qualitys = this.getDataById('756194853578080256');

function baseGet(url) {
    const headers = { "Content-type": "application/json" };
    return fetch(url, { headers }).then(res => res.json())
}

let eventParams;
try {
    eventParams = JSON.parse(data)
} catch (e) {
    console.error(e)
}

console.log(eventParams, 'eventParams-eventParams-eventParams')
if (eventParams) {
    disposition.setEvent(eventParams);
    disposition.setParams(eventParams.id);
    disposition.initQuery();
    // 设置为紧急事件
    gis.widgetObj.artemis.setEmergencyId(eventParams.id);
    // 移动右边 ICONS
    gis.widgetObj.setIconPosition({ right: 570, top: 100 });
    // gis.widgetObj.setModalPosition({ right: 965, bottom: 24, maxWidth: 1910, maxHeight: 960, width: 810, height: 286 })
    gis.widgetObj.setModalPosition({ right: 965, bottom: 24 });
    // 录像回放
    const $playback = this.getApiWidgetByAliasName('playback');
    const { eventDetails } = eventParams;

    console.log('eventDetails', eventDetails);
    if (eventDetails && Object.keys(eventDetails).length > 0) {
        // 关闭三维弹窗
        gis.widgetObj.gisMap.setModalType('NONE');
        // 回放
        const info = JSON.parse(eventDetails);
        console.log('info', info);
        if (info.jpbPath) {
            $playback.widgetObj.setData([{ data: info?.jpbPath }]);
            $playback.showWidget();
            $playback.widgetObj.render();
        } else {
            $playback.hideWidget();
            window.layer.msg('暂无录像回放')
        }

        if (info?.thumbnailPath) {
            const srcArr = info.thumbnailPath.split(',').map(v => ({ src: v }));
            $img.widgetObj.setData([{ data: srcArr }]);
            $img.widgetObj.render();
        } else {
            $img.widgetObj.setData([{ data: [] }]);
            $img.widgetObj.render();
        }

    } else {
        $playback.hideWidget();
        window.layer.msg('暂无录像回放')
    }

    if (eventParams.sourceDeviceId) {
        // 打开实时视频
        baseGet(`/${prefix}Basics/basics-main/system/equipmentObject/listDetailByEquipmentCode?equipmentCode=${eventParams.sourceDeviceId}`)
            .then(({ success, result } = {}) => {
                if (success && result) {
                    const { rows } = result;
                    const data = rows[0]?.equipmentConfig?.gbVideoConfig;
                    var host = this.getVariables().find(x => x.name === "stream_url").value;
                    gis.widgetObj.sullivan.close();
                    if (data && data.sipUser && data.videoChannelId) {
                        gis.widgetObj.sullivan.addPlayer(`${host}/api/v1/stream/rtc/play`, `${data.sipUser}/${data.videoChannelId}`)
                        gis.widgetObj.sullivan.initial()
                    } else {
                        window.layer.msg('暂无设备号/通道号')
                    }
                }
            })
    } else {
        gis.widgetObj.sullivan.close();
        window.layer.msg('未关联摄像头')
    }
    // 获取最新事件详情
    // params.eventPoint = `${params.xCoordinate}，${params.yCoordinate}，${params.zCoordinate}`;

    eventApi.getEvent(eventParams.id).then(res => {
        console.log('eeee-event',  res.result)
        const formData = { ...res.result };
        const whetherProcess = [
            { name: '未处理', value: 0 },
            { name: '已处理', value: 1 },
            { name: '处理中', value: 2 },
            { name: '已解除', value: 3 },
            { name: '误警', value: 4 },
        ];
        // console.log('qualitys', types)
        formData.eventPoint = `${res.result.xCoordinate}，${res.result.yCoordinate}，${res.result.zCoordinate}`;
        $form.widgetObj._option.options = {
            eventTypeCode: types?.data,
            eventGradeCode: levels?.data,
            eventQualityCode: qualitys?.data,
            whetherProcess,
            disableFlag: whetherProcess,
        };

        $form.widgetObj._option.dataSource = formData;
        eventDetail._option.dataSource = formData;
        eventDetail.render();
        $form.widgetObj.render();

        const form = this.retrievePlugin('base-forms');
        form.resetForm('form_14');
        form.resetForm('form_20');
        $form.widgetObj.setFormData('form_14', formData);
    })

    // 渲染按钮组
    this.getApiWidgetByAliasName('modal-btns')?.widgetObj?.renderDispose();
}

window.onmessage = function ({ data } = {}) {
    console.log('<=>', data);
    if (data.name === 'isOther') {
        that.navigateStage('829716080812359680', [data, data.AuthToken])
    }
};
